﻿/**********************************************************\
*  TinyLib - IniFile                                       *
*                                                          *
*  Reads and writes to a text file.                        *
*                                                          *
* (c) Jens Berglind                                        *
*                                                          *
* Last edited by Jens Berglind 25-06-2012                  *
\**********************************************************/ 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace AALib.Input
{
    public class IniFile
    {
        #region Fields

        private readonly string m_filename;
        private Dictionary<string, Dictionary<string, string>>
        m_options = new Dictionary<string, Dictionary<string, string>>();
       
        #endregion

        #region Properties

        public string Filename
        {
            get { return m_filename; }
            // Unused set method  set { filename = value; }
        }

        public string GetValue(string p_group, string p_key)
        {
            return m_options[p_group][p_key];
        }

        #endregion

        #region Init

        public IniFile(string p_filename)
        {
            this.m_filename = p_filename;
        }

        #endregion

        #region Methods

        public void AddModify(string p_group, string p_key, string p_value)
        {
            if (!m_options.ContainsKey(p_group))
            {
                m_options.Add(p_group, new Dictionary<string, string>());
            }

            if (!m_options[p_group].ContainsKey(p_key))
            {
                m_options[p_group].Add(p_key, p_value);
            }

            else
            {
                m_options[p_group][p_key] = p_value;
            }

        }

        public void Parse()
        {
            TextReader iniFile = null;

            if (System.IO.File.Exists(m_filename))
            {
                string line = null;
                string currentGroup = null;
                string[] keyPair = null;
                iniFile = new StreamReader(m_filename);

                line = iniFile.ReadLine();
                while (line != null)
                {
                    if (line != "")
                    {
                        if (line.StartsWith("[") && line.EndsWith("]"))
                        {
                            currentGroup = line.Substring(1, line.Length - 2);
                            m_options.Add(currentGroup, new Dictionary<string, string>());
                        }
                        else
                        {
                            keyPair = line.Split(new char[] { '=' }, 2);
                            m_options[currentGroup].Add(keyPair[0], keyPair[1]);
                        }
                    }
                    line = iniFile.ReadLine();
                }
                iniFile.Close();
            }

        }

        public void Save()
        {
            TextWriter iniFile = new StreamWriter(m_filename);

            foreach (KeyValuePair<string, Dictionary<string, string>> group in m_options)
            {
                iniFile.WriteLine("[" + group.Key + "]");

                foreach (KeyValuePair<string, string> key in group.Value)
                {
                    iniFile.WriteLine(key.Key + "=" + key.Value);
                }
            }
            iniFile.Close();
        }

        #endregion
    }
}
